unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, EncdDecd;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    function DeCompressionStream(const AInStream,AOutStream: TStream) : Boolean;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses Zlib,ZLibEx,ZLibExGZ;
{$R *.dfm}

function TForm1.DeCompressionStream(const AInStream,
  AOutStream: TStream): Boolean;
var
  ds : TDeCompressionStream;
  buff : array[1..4096] of Byte;
  iread : integer;
begin
  Result  :=  True;
  AInStream.Position := 0;
  try
    ds := TDeCompressionStream.Create(AInStream);
    try
      repeat
       iread := ds.Read(buff, sizeof(buff));
       if iread > 0 then
         AOutStream.Write(buff, iread);
      until (iRead = 0);
    finally
      ds.Free;
    end;
  except
    Result  :=  False;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  InStream,OutStream: TStringStream;
begin
{  InStream := TStringStream.Create('');
  OutStream := TStringStream.Create('');
  try
    if DeCompressionStream(InStream,OutStream) then
    begin
      Memo2.Lines.Text := OutStream.DataString;

    end;

  finally
    InStream.Free;
    OutStream.Free;
  end;
}
  Memo2.Lines.Text := GZCompressStr(Memo1.Lines.Text);

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  InStream,OutStream: TStringStream;
  sText : string;
  M : TMemoryStream;
begin
  sText := DecodeString(Memo1.Lines.Text);

  M := TMemoryStream.Create;
  try
    M.Write(sText[1],Length(sText));
    OutStream := TStringStream.Create('');
    try
        m.Position := 0;
        //ZDecompressStream(M, OutStream);
        //GZDecompressStream(M,OutStream);
        DeCompressionStream(M,OutStream);
        Memo2.Lines.Text := OutStream.DataString;


    finally
      OutStream.Free;
    end;
  finally
    M.Free;
  end;


//  Memo2.Lines.Text := GZDecompressStr(sText);
{
  InStream := TStringStream.Create(sText);
  OutStream := TStringStream.Create('');
  try

      ZDecompressStream2(InStream, OutStream, -15);
      Memo2.Lines.Text := OutStream.DataString;


  finally
    InStream.Free;
    OutStream.Free;
  end;
}
end;

function Unzip(const zipped: string): string;
var
  DecompressionStream: TDecompressionStream;
//  Compressed: TBytesStream;
  Decompressed: TStringStream;
  Compressed : TMemoryStream;
  sText : string;
begin
//  Compressed := TBytesStream.Create();
{  sText := DecodeString(AnsiString(zipped));
  Compressed := TMemoryStream.Create;
  try
    Compressed.Write(sText[1],Length(sText));
    // window bits set to 15 + 16 for gzip
    DecompressionStream := TDecompressionStream.Create(Compressed);
    try
      Decompressed := TStringStream.Create('');
      try
        Decompressed.LoadFromStream(DecompressionStream);
        Result := Decompressed.DataString;
      finally
        Decompressed.Free;
      end;
    finally
      DecompressionStream.Free;
    end;
  finally
    Compressed.Free;
  end;
  }
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  //
end;

end.
